<script setup lang="ts">
import {defineProps, reactive, ref} from 'vue'
import {databaseSessionKey, mysqlInterface, mysqlInterface as pool} from '@/util/common'
import {saveDatabase} from "@/util";
import {create} from "domain";

const databaseList = ref<pool[]>([])
const databaseData = reactive<mysqlInterface>({
  host: '', port: 0, database: '', user: '', password: ''
})
let visibleDialog = ref(false)

const databases = localStorage.getItem(databaseSessionKey);
if (databases) databaseList.value = JSON.parse(databases)

const addDatabase = () => {
  saveDatabase(databaseData)
  visibleDialog.value = false
}

</script>

<template>
  <div v-if="databaseList.length !== 0">
    <el-space>
      <el-card class="box-card" v-for="(database, index) of databaseList" :key="index">
        <template #header>
          <div class="card-header">
            <span>{{database.database}}</span>
            <el-button class="button" text>Edit</el-button>
          </div>
        </template>
        <template #default>
          <el-row :gutter="20">
            <el-col :span="6">host: </el-col>
            <el-col :span="12"><span>{{database.host}}</span></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">port:</el-col>
            <el-col :span="12"><span>{{database.port}}</span></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">username:</el-col>
            <el-col :span="12"><span>{{database.user}}</span></el-col>
          </el-row>
        </template>
      </el-card>
    </el-space>
  </div>
  <div v-else>
    <el-card class="box-card">
      <div>没有可用数据库</div>
      <div style="margin-top: 30px;">
        <el-button type="primary" round @click="visibleDialog = true">添加数据库</el-button>
      </div>
    </el-card>
  </div>

  <el-dialog v-model="visibleDialog" title="add database">
    <template #default>
      <el-form :model="databaseData" label-width="80px">
        <el-form-item label="host:">
          <el-col>
            <el-input v-model="databaseData.host" />
          </el-col>
        </el-form-item>
        <el-form-item label="port:">
          <el-input v-model="databaseData.port"/>
        </el-form-item>
        <el-form-item label="database:">
          <el-input v-model="databaseData.database"/>
        </el-form-item>
        <el-form-item label="user:">
          <el-input v-model="databaseData.user"/>
        </el-form-item>
        <el-form-item label="password:">
          <el-input v-model="databaseData.password"/>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="outerVisible = false">Cancel</el-button>
        <el-button type="primary" @click="addDatabase">confirm</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  margin: 20px auto;
  width: 480px;
}
</style>
